#Kyle Bakken, Chelsea Evans, Adam Lamb - Group 6
#CS419-400
#Description: This program will make and send a meeting request to the advisor's outlook inbox
#Input: This program will take as input the Appointment object created from filter.py
#Output: This program will output an email with an .ics attachement to the advisor

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
import re
import objects
import os
import string
import datetime
from datetime import datetime as da, timedelta #this is necessary even if it looks redundant

crlf = "\r\n"
nl = "\n"

def make_date_obj(dt):
	#print dt 	#for testing
	#this will strip trailing letters from the day (IE 18th = 18)
	dt = re.sub(r"(?<=\d)(st|nd|rd|th),", ",", dt)
	#print dt 	#for testing
	#this will change the date string we are given into a date object
	date_object = da.strptime(dt, '%H:%M %A, %B %d, %Y')
	#print date_object	#for testing
	diff = datetime.timedelta(hours = +8)
	date_object = date_object + diff
	cal_object = date_object.strftime("%Y%m%dT%H%M%SZ")

	return cal_object 

def get_time(time_ob):
	#print time_ob	#for testing
	time = str(time_ob)
	#print time 	#for testing
	start = 0
	end = time.find(':')
	hour = time[start:end]
	#print "preHour: " + hour	#for testing
	start = time.find(':') + 1
	end = time.find('\n') - 1
	minute = time[start:end]
	minute = minute.strip('m')
	minute = minute.strip('p')
	minute = minute.strip('a')
	#print "minute: " + minute	#for testing
	hour = int(hour)
	if time.find('am') == -1:
		if time.find('pm') != -1:
			hour = hour + 12
	hour = str(hour)
	hour = hour.rjust(2, '0')
	#print "postHour: " + hour 	#for testing
	new_time = hour + ':' + minute
	#print new_time	#for testing
	return new_time


def outlook_new(app):
	#Accept input from filter.py
	#print 'outlook script executed!'	#for testing

	#make .ics object
	dt1 = get_time(app.start_time) + ' ' + app.date
	dt1 = dt1.strip(' \r\n')
	dt2 = str(get_time(app.end_time)) + ' ' + str(app.date)
	dt2 = dt2.strip(' \r\n')
	start_time = make_date_obj(dt1)
	end_time = make_date_obj(dt2)
	dtstamp = da.now().strftime("%Y%m%dT%H%M%SZ")

	ical = 'BEGIN:VCALENDAR' + crlf 
	ical += 'PRODID:-//OSUCS419F14//Group6//EN' + crlf
	ical += 'VERSION:2.0' + crlf 
	ical += 'CALSCALE:GREGORIAN' + crlf
	ical += 'METHOD:REQUEST' + crlf 
	ical += 'BEGIN:VEVENT' + crlf 
	ical += 'DTSTART:' + start_time + crlf 
	ical += 'DTEND:' + end_time + crlf 
	ical += 'DTSTAMP:' + dtstamp + crlf
	ical += 'ORGANIZER:' + app.advisee + crlf
	ical += 'UID:4321' + crlf  
	ical += 'CREATED:' + dtstamp + crlf 
	ical += 'SEQUENCE:0' + crlf
	ical += 'SUMMARY:Advising Meeting' + crlf
	ical += 'DESCRIPTION:Advising Meeting' + crlf 
	ical += 'STATUS:CONFIRMED' + crlf
	ical += "END:VEVENT"+ crlf +"END:VCALENDAR"+ crlf
	#print ical	#for testing


	#create MIME message
	msg = MIMEMultipart()
	msg['Subject'] = app.subject
	msg['From'] = app.origin_email
	msg['To'] = app.advisor_email

	body = 'WHAT: ' + app.subject + nl + 'FOR: ' + app.advisor + nl + 'WITH: '
	body += app.advisee + nl + 'WHEN: ' + app.date + ' ' + app.start_time
	body += ' - ' + app.end_time + nl

	part_email = MIMEText(body,"html")
	part_cal = MIMEText(ical,'calendar;method=CANCEL')

	msg.attach(part_email)
	msg.attach(part_cal)

	#send email

	smtp = smtplib.SMTP('mail.oregonstate.edu', 587);
	smtp.sendmail(str(app.origin_email), str(app.advisor_email), msg.as_string())
	smtp.quit()

def outlook_cancel(app):
	#print 'outlook script executed!'	#for testing

	#make .ics object
	dt1 = get_time(app.start_time) + ' ' + app.date
	dt1 = dt1.strip(' \r\n')
	dt2 = str(get_time(app.end_time)) + ' ' + str(app.date)
	dt2 = dt2.strip(' \r\n')
	start_time = make_date_obj(dt1)
	end_time = make_date_obj(dt2)
	dtstamp = da.now().strftime("%Y%m%dT%H%M%SZ")

	ical = 'BEGIN:VCALENDAR' + crlf 
	ical += 'PRODID:-//OSUCS419F14//Group6//EN' + crlf
	ical += 'VERSION:2.0' + crlf
	ical += 'CALSCALE:GREGORIAN' + crlf 
	ical += 'METHOD:CANCEL' + crlf
	ical += 'BEGIN:VEVENT' + crlf
	ical += 'DTSTART:' + start_time + crlf 
	ical += 'DTEND:' + end_time + crlf  
	ical += 'DTSTAMP:' + dtstamp + crlf
	ical += 'ORGANIZER:' + app.advisee + crlf
	ical += 'UID:4321' + crlf  
	ical += 'CREATED:' + dtstamp + crlf	 
	ical += 'SEQUENCE:0' + crlf	 
	ical += 'SUMMARY:Advising Meeting' + crlf
	ical += 'DESCRIPTION:Advising Meeting' + crlf 
	ical += 'STATUS:CANCELLED' + crlf
	ical += "END:VEVENT"+ crlf +"END:VCALENDAR"+ crlf
	#print ical	#for testing


	#create MIME message
	msg = MIMEMultipart()
	msg['Subject'] = app.subject
	msg['From'] = app.origin_email
	msg['To'] = app.advisor_email

	body = 'WHAT: ' + app.subject + nl + 'FOR: ' + app.advisor + nl + 'WITH: '
	body += app.advisee + nl + 'WHEN: ' + app.date + ' ' + app.start_time
	body += ' - ' + app.end_time + nl

	part_email = MIMEText(body,"html")
	part_cal = MIMEText(ical,'calendar;method=CANCEL')

	msg.attach(part_email)
	msg.attach(part_cal)

	#send email

	smtp = smtplib.SMTP('mail.oregonstate.edu', 587);
	smtp.sendmail(str(app.origin_email), str(app.advisor_email), msg.as_string())
	smtp.quit()